package top.rish.minfire.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.NvMap;
import org.noear.solon.core.util.ResourceUtil;

import java.io.File;
import java.net.URL;


@Configuration
@Inject("${app-center}")
public class AppCenterConfig {

    String adminUiDir;
    String configYml;


    private static final String CONFIG_YML = "app-center.config-yml";
    private static final String ADMIN_UI_DIR = "app-center.admin-ui-dir";
    private static final String EXTEND_DIR = "app-center.extend-dir";
    private static final String APP_YML = "app.yml";
    private static final String ADMIN_STATIC_DIR = "static";
    private static final String SOLON_DEBUG="solon.debug";
    private static final String SOLON_CONFIG_COMMON ="config";
    private static final String SOLON_EXTEND_COMMON ="extend";
    private static final String SOLON_VIEW_PREFIX = "solon.view.prefix";

    public static void mergeConfig(NvMap nvMap) {
        putAndDefault(nvMap, SOLON_DEBUG, "1");
        putAndDefault(nvMap, ADMIN_UI_DIR, "app-center-admin");
        putAndDefault(nvMap, EXTEND_DIR, "app-center-admin-extend");
        putAndDefault(nvMap, CONFIG_YML, APP_YML);
        String solonConfigLoad = nvMap.get(SOLON_CONFIG_COMMON);
        String adminUiDir = nvMap.get(ADMIN_UI_DIR);
        String configYml = nvMap.get(CONFIG_YML);
        String extendDir = nvMap.get(EXTEND_DIR);
        String userDir = System.getProperty("user.dir");
        String extendDirFile = String.format("%s/%s/",userDir,extendDir);
        String uiDir = String.format("file:%s/%s/",userDir,adminUiDir);
        putAndDefault(nvMap, SOLON_VIEW_PREFIX, uiDir);
        String template =
                String.format("%s/%s/%s",userDir,adminUiDir,configYml);
        String staticDir = String.format("%s/%s/%s/",userDir,adminUiDir,ADMIN_STATIC_DIR);
        FileUtil.mkdir(staticDir);
        String configPath =
                String.format("file:%s",template);
        String extendPath =
                String.format("file:%s",template);
        FileUtil.mkdir(extendDirFile);
        FileUtil.mkParentDirs(template);
        File configYmlFile = new File(template);
        if (!configYmlFile.exists() || !configYmlFile.isFile()) {
            FileUtil.touch(configYmlFile);
            URL resource = ResourceUtil.findResource(String.format("classpath:./%s",APP_YML));
            String configContent = FileUtil.readUtf8String(resource.toString());
            FileUtil.writeUtf8String(configContent, configYmlFile);
        }
        if (ObjectUtil.isNotEmpty(solonConfigLoad)) {
            solonConfigLoad = solonConfigLoad + "," + configPath;
        } else {
            solonConfigLoad = configPath;
        }
        nvMap.put(SOLON_CONFIG_COMMON, solonConfigLoad);
        nvMap.put(SOLON_EXTEND_COMMON, extendPath);
    }

    private static void putAndDefault(NvMap nvMap, String k, String dv) {
        if (ObjectUtil.isEmpty(nvMap.get(k))) {
            nvMap.put(k, dv);
        }
    }



}
